<?php
/**
 * file helpers
 *
 * @author alex_ez
 */

function file_mime( $file )
{
	$magicfile = config::instance()->fileinfo__magicfile;
	if( !is_readable( $magicfile ) ) $magicfile = null;
	$fi = @new finfo( FILEINFO_MIME, $magicfile );
	$mime = @$fi ->file( $file );
	unset( $fi );
	
	return $mime;
}

function file_mime_array( $file )
{
	$r = http_parse_params_array( file_mime( $file ) );
	$r['type'] = array_shift( $r );
	foreach( $r as $k => $v )
		if( is_int($k) && is_array( $v ) )
		{
			foreach($r[$k]as$sk=>$sv)$r[$sk]=$sv;
			unset($r[$k]);
		}
	
	return $r;
}

function size_format($size, $units = false) {
	
    $mod = 1024;

	if(empty($units))
		$units = array(__('B'), __('KB'), __('MB'), __('GB'), __('TB'), __('PB'));

    for ($i = 0; $size > $mod; $i++) {
        $size /= $mod;
    }

    return number_format($size, $size == (int)$size ? 0 : 2) . (isset($units[$i]) ? (' ' . $units[$i]) : '');
}
